package com.jerry.walking.module.home.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jerry.walking.R;
import com.jerry.walking.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Yang on 2016/5/9.
 *
 *            欢迎界面
 */
public class WelcomeActivity extends BaseActivity{

    @BindView(R.id.iv_welcome)
    ImageView imageView;

    @BindView(R.id.vp_welcome)
    ViewPager viewPager;

    Handler handler = new Handler();

    //假设为第一次登录
    private boolean isFirst = true;

    //开启图片资源
    private int[] images = new int[]{
            R.mipmap.img_lead_one,
            R.mipmap.img_lead_two
    };
    private List<ImageView> list;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_welcome;
    }

    @Override
    protected void initViews() {
        super.initViews();

        SharedPreferences sp = getSharedPreferences("isFirstRun", MODE_PRIVATE);
        isFirst = sp.getBoolean("isFirstRun", true);

        if (isFirst) {
            //第一次登录
            //记录并设置图片消失
            imageView.setVisibility(View.GONE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putBoolean("isFirstRun", false);
            editor.commit();

            list = new ArrayList<>();

            //添加图片
            getImages();

            viewPager.setAdapter(new PagerAdapter() {
                @Override
                public int getCount() {
                    return images.length;
                }

                @Override
                public boolean isViewFromObject(View view, Object object) {
                    return view == object;
                }

                @Override
                public Object instantiateItem(ViewGroup container, int position) {

                    container.addView(list.get(position));

                    return list.get(position);
                }

                @Override
                public void destroyItem(ViewGroup container, int position, Object object) {
//                    super.destroyItem(container, position, object);
                    container.removeView((View) object);
                }
            });
        } else {
            //普通登录
            imageView.setVisibility(View.VISIBLE);
            goToWelcome();
        }
    }

    /**
     * 打开延迟欢迎界面 ---> 并且打开主界面
     */
    private void goToWelcome() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(intent);
                finish();
            }
        }, 3000);
    }

    private void getImages() {
        for (int i = 0; i < images.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setImageResource(images[i]);

            //对最后一张图加上监听器
            if (i == images.length - 1) {
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        startActivity(intent);
                        finish();
                    }
                });
            }

            list.add(imageView);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}